package com.uchengwang.ui.cm_ui.cycle.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.CommentConfig;
import com.uchengwang.ui.cm_ui.cycle.bean.CommentItem;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleSubPeopleSharesBean;
import com.uchengwang.ui.cm_ui.cycle.bean.H5ReportBean;
import com.uchengwang.ui.cm_ui.cycle.bean.H5ShareBean;
import com.uchengwang.ui.cm_ui.cycle.bean.SetupMenusBean;
import com.uchengwang.ui.cm_ui.cycle.bridge.CirclePresenter;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.PlayVideoCustomActivity;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.ArticleViewHolder;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.CircleViewHolder;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.ImageViewHolder;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.URLViewHolder;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.VideoViewHolder;
import com.uchengwang.ui.utils.DateUtil;
import com.uchengwang.ui.utils.MoreOperateUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.view.CommentDialog;
import com.uchengwang.ui.view.CommentListView;
import com.uchengwang.ui.view.MultiImageView;

import java.util.List;

/**
 * Created by U城网 on 2018/6/19.
 */

public class FollowAdapter extends BaseRecycleViewAdapter<CycleSubPeopleSharesBean, RecyclerView.ViewHolder>{
    private Context context;

    private CirclePresenter presenter;
    private View bottomView;


    public FollowAdapter (Context context){
        this.context = context;
    }
    public void setCirclePresenter(CirclePresenter presenter){
        this.presenter = presenter;
    }

    public void setBottomView(View bottomView){
        this.bottomView = bottomView;
    }


    @Override
    public int getItemViewType(int position) {

        return datas.get(position).getShareType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_follow_layout, parent, false);
        RecyclerView.ViewHolder viewHolder = null;
        //1：享说图文 2：投票  3：活动  4：会场  5：享说视频  6：链接  7：文章
        switch (viewType){
            case 1://图文
                viewHolder = new ImageViewHolder(view);
                break;
            case 2:
                viewHolder = new ImageViewHolder(view);
                break;
            case 3:
                viewHolder = new ImageViewHolder(view);
                break;
            case 4:
                viewHolder = new ImageViewHolder(view);
                break;
            case 5://视频
                viewHolder = new VideoViewHolder(view);
                break;
            case 6://连接
                viewHolder = new URLViewHolder(view);
                break;
            case 7://文章
                viewHolder = new ArticleViewHolder(view);
                break;
            default:
                viewHolder = new ImageViewHolder(view);
                break;

        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {
        final CircleViewHolder holder = (CircleViewHolder) h;
        final CycleSubPeopleSharesBean bean = datas.get(position);

        final List<CommentItem> commentItems = bean.getCommentItemLists();

        ImageManager.getInstance().loadImage(context, holder.headIv, bean.getUserAvatar(), ImageManager.ImageType.CIRCLE);

        holder.nameTv.setText(bean.getNickName());
        holder.timeTv.setText(DateUtil.getDateUtil().getTimeDateWx(bean.getCreateTime()));

        if (bean.isLike()){
            ImageManager.getInstance().loadImage(context, holder.like_image, R.drawable.like_yes, ImageManager.ImageType.NARMOL);
        }else {
            ImageManager.getInstance().loadImage(context, holder.like_image, R.drawable.like_no, ImageManager.ImageType.NARMOL);
        }


        switch (holder.viewType) {
            case CircleViewHolder.TYPE_IMAGE:// 处理图片
                holder.contentTv.setVisibility(View.VISIBLE);
                holder.contentTv.setText(bean.getShareContent());

                ((ImageViewHolder)holder).multiImageView.setIsVideo(false);
                final List<String> photos = bean.getPhotos();
                if (photos != null && photos.size() > 0) {
                    if (photos.size() ==1){
                        ((ImageViewHolder)holder).multiImageView.setWidthAndHeight(bean.getImgWidth(), bean.getImgHeight());
                    }

                    ((ImageViewHolder)holder).multiImageView.setVisibility(View.VISIBLE);
                    ((ImageViewHolder)holder).multiImageView.setList(photos);
                    ((ImageViewHolder)holder).multiImageView.setOnItemClickListener(new MultiImageView.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            UCommonUtil.getUtil().startImagePagerActivity(context, OSSManager.OSS_SHARE, photos,  position, false);
                        }
                    });
                } else {
                    ((ImageViewHolder)holder).multiImageView.setVisibility(View.GONE);
                }
                break;

            case CircleViewHolder.TYPE_VIDEO:
                holder.contentTv.setVisibility(View.VISIBLE);
                holder.contentTv.setText(bean.getShareContent());

                if(holder instanceof VideoViewHolder){
//                    ((VideoViewHolder)holder).videoView.setVideoUrl(bean.getVideoJson());
//                    ((VideoViewHolder)holder).videoView.setVideoCoverUrl(bean.getShareCover());//视频封面图片
//                    ((VideoViewHolder)holder).videoView.setPostion(position);
                    Log.d("jjf", "视频封面图=="+bean.getShareCover());
                    ImageManager.getInstance().setRoundSize(1).loadImage(context, ((VideoViewHolder) holder).video_cover, bean.getShareCover(), ImageManager.ImageType.ROUND);
                    ((VideoViewHolder) holder).video_cover.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {Intent intent =new Intent(context, PlayVideoCustomActivity.class);
                            intent.putExtra(BaseConfig.VIDEO_ID, bean.getVideoJson());
                            context.startActivity(intent);
                        }
                    });
                }
                break;

                case CircleViewHolder.TYPE_URL://链接
                    holder.contentTv.setVisibility(View.GONE);
                    if(holder instanceof URLViewHolder){
                        ((URLViewHolder) holder).url_title.setText(bean.getShareTitle());
                        ImageManager.getInstance().loadImage(context, ((URLViewHolder) holder).url_cover, bean.getShareCover(), ImageManager.ImageType.ROUND);

                        ((URLViewHolder) holder).urlBody.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                                intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                                intent.putExtra(BaseConfig.URL_TITLE, bean.getShareTitle());
                                intent.putExtra(BaseConfig.URL, bean.getShareUrl());
                                context.startActivity(intent);
                            }
                        });

                }
                break;
            case CircleViewHolder.TYPE_ARTICLE://文章
                holder.contentTv.setVisibility(View.VISIBLE);
                holder.contentTv.setText("发布了文章");

                if(holder instanceof ArticleViewHolder){
                    ((ArticleViewHolder) holder).article_title.setText(bean.getArticle().getArticleTitle());
                    ImageManager.getInstance().loadImage(context, ((ArticleViewHolder) holder).article_cover, bean.getArticle().getArticleCover(), ImageManager.ImageType.ROUND);

                    ((ArticleViewHolder) holder).article_ln.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                            intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                            intent.putExtra(BaseConfig.URL_TITLE, bean.getShareTitle());
                            intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/article/" + bean.getArticle().getId());
                            context.startActivity(intent);
                        }
                    });

                }
                break;

        }

        if (commentItems != null && commentItems.size() >0){
            holder.commentList.setDatas(commentItems);
            holder.commentList.setVisibility(View.VISIBLE);
            holder.commentList.setOnItemClickListener(new CommentListView.OnItemClickListener() {
                        @Override
                        public void onItemClick(int commentPosition) {
                            CommentItem commentItem = commentItems.get(commentPosition);
                            if(UCommonUtil.getUtil().getId() == commentItem.getUser().getuId()){//复制或者删除自己的评论
                                CommentDialog dialog = new CommentDialog(context, presenter, commentItem, position);
                                if (commentItem.getToReplyUser() == null){
                                    //说明删除的是评论
                                    dialog.setType(CirclePresenter.DELETE_CMT);
                                }else {
                                    dialog.setType(CirclePresenter.DELETE_REPLY);
                                }
                                dialog.show();
                            }else{//回复别人的评论
                                if(presenter != null){
                                    CommentConfig config = new CommentConfig();
                                    config.circlePosition = position;
                                    config.commentPosition = commentPosition;
                                    config.commentType = CirclePresenter.ADD_REPLY;

                                    if (commentItem.getToReplyUser() == null){
                                        //说明对评论进行回复
                                        config.cmtId = commentItem.getId();
                                    }else {
                                        //说明对回复进行回复
                                        config.cmtId = commentItem.getCmtId();
                                        config.replyId = commentItem.getId();
                                    }

                                    config.replyUser = commentItem.getUser();
                                    presenter.showEditTextBody(config);
                                }
                            }
                        }
                    });
        }else {
            holder.commentList.setVisibility(View.GONE);
        }

        holder.cmt_ln.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(presenter != null){
                    CommentConfig config = new CommentConfig();
                    config.circlePosition = position;
                    config.commentType = CirclePresenter.ADD_CMT;
                    config.shareId = bean.getId();

                    presenter.showEditTextBody(config);
                }
            }
        });

        holder.headIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/"+bean.getUid());
                context.startActivity(intent);
            }
        });

        holder.like_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CycleHttpUtils.getUtils().addLikeOrDelete((Activity) context, bean.isLike(), bean.getId(), new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            //操作成功
                            if (bean.isLike()){
                                //取消点赞成功
                                ImageManager.getInstance().loadImage(context, holder.like_image, R.drawable.like_no, ImageManager.ImageType.NARMOL);
                            }else {
                                //点赞成功
                                ImageManager.getInstance().loadImage(context, holder.like_image, R.drawable.like_yes, ImageManager.ImageType.NARMOL);
                            }
                            bean.setLike(!bean.isLike());
                        }else {
                            UCommonUtil.getUtil().showToast(context, "失败");
                        }
                    }
                });
            }
        });


        holder.follow_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetupMenusBean setupMenusBean = new SetupMenusBean();
                H5ShareBean h5ShareBean = new H5ShareBean();
                H5ReportBean h5ReportBean = new H5ReportBean();

                switch (bean.getShareType()){
                    case BaseConfig.SHARE_XIANG_SHUO:
                    case BaseConfig.SHARE_VIDEO:

                        h5ShareBean.setCover(bean.getShareCover());
                        h5ShareBean.setTitle(bean.getShareTitle());
                        h5ShareBean.setDesc(bean.getShareContent());
                        h5ShareBean.setUrl(BaseConfig.BaseWebURL + "/share/" + bean.getId());

                        h5ReportBean.setReferId(bean.getId());
                        break;

                    case BaseConfig.SHARE_ARTICLE:
                        h5ShareBean.setCover(bean.getArticle().getArticleCover());
                        h5ShareBean.setTitle(bean.getArticle().getArticleTitle());
                        h5ShareBean.setDesc(bean.getArticle().getArticleContent());
                        h5ShareBean.setUrl(BaseConfig.BaseWebURL + "/article/" + bean.getId());

                        h5ReportBean.setReferId(bean.getArticle().getId());
                    break;
                }

                setupMenusBean.setShare(h5ShareBean);

                h5ReportBean.setReferType(BaseConfig.SHARE_ARTICLE);
                h5ReportBean.setReportUid(bean.getUid());
                h5ReportBean.setReportNickName(bean.getNickName());
                h5ReportBean.setReportUserAvatar(bean.getUserAvatar());
                setupMenusBean.setReport(h5ReportBean);

                MoreOperateUtils.getUtils().more((Activity) context, bottomView, setupMenusBean);
            }
        });

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


}
